﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media.Media3D;

namespace Wen.UI.NavigationElements
{
	internal class DirectionalLightViewModel : IndicatorLightViewModelBase<DirectionalLight>
	{
		private readonly LightIndicator _directionIndicator;
		private readonly ProjectionCamera _camera;

		private readonly DirectionalLightDirectionViewModel _directionalLightDirectionViewModel;
		public DirectionalLightDirectionViewModel DirectionViewModel {
			get { return _directionalLightDirectionViewModel; }
		}

		public DirectionalLightViewModel(DirectionalLight light, Viewport3D viewport3D)
			: base(light, viewport3D) {
			_camera = (ProjectionCamera)viewport3D.Camera;
			_camera.Changed += (s, e) => RefreshIndicatorsPosition();
			_directionalLightDirectionViewModel = new DirectionalLightDirectionViewModel(light);
			_directionalLightDirectionViewModel.ValueChanged += (s, e) => RefreshIndicatorsPosition();
			_directionIndicator = new LightIndicator(viewport3D, true, IndicatorPosition);
		}

		private Point3D IndicatorPosition {
			get {
				return new Point3D(_camera.Position.X + Light.Direction.X, _camera.Position.Y + Light.Direction.Y, _camera.Position.Z + Light.Direction.Z);
			}
		}

		protected override IEnumerable<LightIndicator> Indicators {
			get { yield return _directionIndicator; }
		}

		protected override void RefreshIndicatorsPosition() {
			_directionIndicator.Position = IndicatorPosition;
		}
	}
}
